<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="com.stone.para.IConstant"%>
<%
	String contextPath = request.getContextPath();
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=IConstant.PROGRAEM_TITLE%></title>
        <link rel="stylesheet" href="<%=contextPath%>/ext3/resources/css/ext-all.css" type="text/css"   />
        <link rel="stylesheet" href="<%=contextPath%>/css/main.css" type="text/css"  />
        <script type="text/javascript" src="<%=contextPath%>/js/jquery-1.3.2.min.js"></script> 
        <script src="<%=contextPath%>/ext3/adapter/ext/ext-base.js"></script>
        <script src="<%=contextPath%>/ext3/ext-all.js"></script>
        <script language="javascript">
            Ext.BLANK_IMAGE_URL = '<%=contextPath%>/ext3/resources/images/default/s.gif';
            Ext.QuickTips.init();
            Ext.form.Field.prototype.msgTarget = 'qtip';
            var cpath = '<%=contextPath%>';
        </script>
        
		<script language="javascript">
		
		var addForm;
		var comboStore;
		
		Ext.onReady(function(){
		    Ext.lib.Ajax.defaultPostHeader += '; charset=utf-8';
		    var myNode;
		    
  			/* 生成Ttee,放到tree Div里面 */
            var tree = new Ext.tree.TreePanel({
                        id:"mytree",   
                        root : new Ext.tree.AsyncTreeNode({   
                    		id : "0", 
                    		draggable:false, 
                    		singleClickExpand:true, 
                    		text : "<%=IConstant.PROGRAEM_TITLE%>绩效考核细则管理",
                    		qtip:'100'
	 					}),   
 						loader:new Ext.tree.TreeLoader({
                    		url : 'menu!getMenu.do?parentid=',
                    		listeners:{"beforeload":function(loader, node){ 
            						loader.url ='jxkhxz!getData.do?parentid=' + node.id ;
                    			}}
               		 	}),
                        //rootVisible:false,
				        useArrows:true,
				        autoScroll:true,
				        //animate:true,
				       // enableDD:true,	
						tbar : [ {
							id :'addDicButton',
							text :'增加',
				            cls:'x-btn-text-icon',
				            icon:cpath + '/images/add.gif',
							tooltip :'添加一条记录',
							handler:addButtonClick
						}, '-', {
							id :'updateDicButton',
							text :'修改',
				            cls:'x-btn-text-icon',
				            icon:cpath + '/images/edit.gif',
							tooltip :'修改所选记录',
							handler:updateButtonClick
						}, '-', {
							id :'deleteDicButton',
							text :'删除',
				            cls:'x-btn-text-icon',
				            icon:cpath + '/images/delete.gif',
							tooltip :'删除所选记录',
							handler:delButtonClick
						} ],
				        containerScroll: true
                    });

            
         	
            		/* 单击 */ 
            		tree.on("click",function(node) {
            			myNode = node;
            		});
				            tree.on('click',function(node){
				            
				            	myNode = node;
				            	
				                if (node.isLeaf() != undefined && node.isLeaf()){
				          
				                    //alert(Ext.get('main').dom.src);
				                } else {
				                	if (!node.isExpanded()) {
				                		node.expand();		
				                	} else {
				                		node.collapse();
				                	}              
				                }
				            });
            
            
            
            
            /* 双击 */
            tree.on("dblclick",function(node) {
            
            	myNode = node
            	// 修改
            	updateButtonClick();

            });
            
            /* Tree绑定菜单 */
			tree.on('contextmenu',function (node,e) {
			    myNode = node;
      			e.preventDefault();   
        		memu.showAt(e.getXY());   
			});
            
            /* 右键事件 */
		    var memu = new Ext.menu.Menu({   
		        allowOtherMenus:true,   
		        items:[{   
		            text:'添加',
		            handler:addButtonClick  
		        },{   
		            text:'修改',
		            handler:updateButtonClick    
		        },{   
		            text:'删除',
		            handler:delButtonClick  
		        }]   
		    });   
		    
		    	var layout = new Ext.Viewport({   
	                layout: 'border',   
	                margins:'5 0 5 5',   
	                items: [ { region: 'center',   
	                    title: '绩效考核细则管理',
	                    height:800,
	                    layout:'fit',
	                    items:[tree]  
	                }]   
	            }); 

			// 添加
			var addDicWin;
		
			// 点击添加按钮时触发的事件
			function addButtonClick() {
			
				if (myNode == undefined) {
					Ext.MessageBox.alert('提示','请您首先选择考核项目！');
					return;
				}

				// 判断window是否存在，不存在创建window
				if (!addDicWin) {
					
					addDicWin = new Ext.Window( {
						title :'绩效考核细则管理',
						layout :'fit',
						width :450,
						height :280,
						closeAction :'hide',
						plain :true,
						modal :true,
						items :[addForm],buttons:[{text:'提交',id:'btnSubmit', handler:formSubmit},
						                          {text:'关闭',id:'btnClose',handler:winClose}]
					});
				
				}
				
				addForm.getForm().findField("entity.id").setValue("0");
				addForm.getForm().findField("entity.parentid").setValue(myNode.id);
				addForm.getForm().findField("entity.mkname").setValue("");
				addForm.getForm().findField("entity.kpxm").setValue("");
				addForm.getForm().findField("entity.kpnr").setValue("");
				addForm.getForm().findField("entity.jjfz").setValue("");				
				addDicWin.show(this);
	
			}
			
			// 添加form 
			addForm = new Ext.FormPanel({
				frame :true,
				labelAlign :'right',
				waitMsgTarget :true,
				autoScroll :true,
				buttonAlign :'center',
				method :'POST',
				url :'jxkhxz!save.do',
				items : [{
					xtype :'textfield',
					fieldLabel :'模块名称',
					name :'entity.mkname',
					maxLength :25,
					width :100
				},  {
					xtype :'textfield',
					fieldLabel :'考评项目',
					name :'entity.kpxm',
					maxLength :50,
					width :150
				},{
					xtype :'textarea',
					height:100,
					fieldLabel :'考评内容',
					name :'entity.kpnr',
					maxLength :250,
					width :250
				}, {
					xtype :'textfield',
					fieldLabel :'加减分值',
					name :'entity.jjfz',
					maxLength :20,
					width :250
				},{
					xtype :'hidden',
					name :'entity.id',
					value :'0'
				},{
					xtype :'hidden',
					name :'entity.parentid'
				}]
			});

			// 验证数据并提交
			 function formSubmit() {
			 
				if (addForm.form.isValid()) {
					addForm.getForm().submit({
						waitMsg :'保存数据...',
						success : function(form, action) {
							if (action.result.success) {
								Ext.MessageBox.alert('提示 ', "保存成功！");
								addDicWin.hide();
								
								if (myNode.id == "0") {
									tree.root.reload();
								} else if (myNode.parentNode != null) {
			           				myNode.parentNode.reload();
			           			}
							}
						},
						failure : function() {
							Ext.MessageBox.alert('提示', '保存失败！');
							addDicWin.hide();
						}
					});
				}
			};
	
			// 关闭window
			function winClose() {
				addDicWin.hide();
			};
		
			// 删除
			function delButtonClick() {

				if (myNode == undefined ||myNode.id == "0") {
					Ext.MessageBox.alert('提示','请您首先选择要删除的纪录！');
					return;
				}

				if (!myNode.leaf) {
					Ext.MessageBox.alert('提示','【' + myNode.text + '】有下级纪录不能删除。');
					return;
				}
				Ext.MessageBox.confirm('消息', '确认要删除所选纪录?', doDelProc);

			};
		
			// 确定要删除后执行事件
			function doDelProc(btn) {

				if (btn == 'yes') {

					Ext.Ajax.request({
						method :'POST',
						url :'jxkhxz!delete.do',
						params : {id :myNode.id},
						success : function(response) {

								if (myNode.parentNode.childNodes.length == 1) {
									if (myNode.parentNode.parentNode != null) {
										myNode.parentNode.parentNode.reload();
									} else {
										myNode.parentNode.reload();
									}
								} else {
		           					myNode.parentNode.reload();
		           				}
							Ext.MessageBox.alert('提示', "删除成功！");
						},
						failure : function() {
							Ext.MessageBox.alert('提示', '数据删除失败！');
						}
					});
				}
			};
		
			// 修改
			function updateButtonClick() {
			
				if (myNode == undefined ||myNode.id == "0") {
					Ext.MessageBox.alert('提示','请您首先选择要修改的数据！');
					return;
				}

				Ext.Ajax.request({
					method :'POST',
					url :'jxkhxz!find.do',
					params : {
						id :myNode.id
					},
					success : function(response, options) { 
						var obj = Ext.decode(response.responseText);
						if (Ext.isIE) {
							document.getElementById("addDicButton").click();
						} else {
							addButtonClick();
						}
						
						// 页面控件赋值
						for (var p in obj) {
							try {
								addForm.getForm().findField("entity." + p).setValue(obj[p]);
							} catch (e){
							}
						}						
					},
					failure : function() {
						Ext.MessageBox.alert('提示 ', '提取数据失败！');
					}
				});
			}
     });
  
		</script>
	</head>
</html>
